home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GDEVGIF.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  13KB  |  434 lines

  1. /* Copyright (C) 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevgif.c */
  20. /* GIF output device for Ghostscript. */
  21. #include "gdevprn.h"
  22. #include "gserrors.h"
  23. #include "gdevpccm.h"
  24.  
  25. /* Thanks to Phil Conrad for donating the original version */
  26. /* of these drivers to Aladdin Enterprises. */
  27.  
  28. /* ------ The device descriptors ------ */
  29.  
  30. /*
  31.  * Default X and Y resolution.
  32.  */
  33. #define X_DPI 72
  34. #define Y_DPI 72
  35.  
  36. /* The same print_page routine currently serves for */
  37. /* both monochrome and color. */
  38. private dev_proc_open_device(gif_open);
  39. private dev_proc_print_page(gif_print_page);
  40. private dev_proc_close_device(gif_close);
  41.  
  42. /* Monochrome. */
  43.  
  44. private gx_device_procs gifmono_procs =
  45.   prn_procs(gif_open, gdev_prn_output_page, gif_close);
  46. gx_device_printer far_data gs_gifmono_device =
  47.   prn_device(gifmono_procs, "gifmono",
  48.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  49.     X_DPI, Y_DPI,
  50.     0,0,0,0,            /* margins */
  51.     1, gif_print_page);
  52.  
  53. /* Chunky 8-bit (SuperVGA-style) color. */
  54. /* (Uses a fixed palette of 3,3,2 bits.) */
  55.  
  56. private gx_device_procs gif8_procs =
  57.   prn_color_procs(gif_open, gdev_prn_output_page, gif_close,
  58.     pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  59. gx_device_printer far_data gs_gif8_device =
  60.   prn_device(gif8_procs, "gif8",
  61.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  62.     X_DPI, Y_DPI,
  63.     0,0,0,0,            /* margins */
  64.     8, gif_print_page);
  65.  
  66. /* ------ Private definitions ------ */
  67.  
  68. /* All two-byte quantities are stored LSB-first! */
  69. #if arch_is_big_endian
  70. #  define assign_ushort(a,v) a = ((v) >> 8) + ((v) << 8)
  71. #else
  72. #  define assign_ushort(a,v) a = (v)
  73. #endif
  74.  
  75. typedef struct gif_header_s {
  76.     byte    signature[3];    /* magic number == 'GIF' */
  77.     byte    version[3];    /* version # '87a' or '89a' */
  78.     ushort    width;        /* screen width */
  79.     ushort    height;        /* screen height */
  80.  
  81. /*    struct    {        /* bit structure of flags */
  82. /*    unsigned globalcolor:1;    /* global color table flag - MSB*/
  83. #define globalcolor_shift 7
  84. /*    unsigned colorres:3;    /* bits/color */
  85. #define colorres_shift 4
  86. /*    unsigned sort:1;        /* color table sorted */
  87. #define sort_shift 3
  88. /*    unsigned colorsize:3;    /* 2^colorsize bytes in color table -LSB */
  89. #define colorsize_shift 0
  90. /*    } flags;        */
  91.  
  92.     byte     flags;
  93.     byte    background;    /* background color index */
  94.     byte    aspect;        /* pixel aspect ratio */
  95.                 /* ratio = (aspect + 15) / 64 */
  96. } gif_header;
  97.  
  98. typedef struct image_descriptor_s {
  99. /*    byte    separator;    /* image separator == 0x2c */
  100.     ushort    left_pos;    /* image left pos (pixels) */
  101.     ushort    top_pos;    /* image top  pos (pixels) */
  102.     ushort    width;        /* image width    (pixels) */
  103.     ushort    height;        /* image height   (pixels) */
  104.  
  105. /*    struct    {        */
  106. /*    unsigned localcolor:1;    /* local color table flag */
  107. /*    unsigned interlace:1;    /* image interlaced  0=no */
  108. /*    unsigned sort:1;    /* color table sorted 0=no*/
  109. /*    unsigned resv:2;    */
  110. /*    unsigned localsize:3;    /* 2^localsize+1 = color table size */
  111. /*    } flags;        */
  112.  
  113.     byte    flags;
  114. } image_descriptor;
  115.  
  116. /********************************************************/
  117. /* LZW routines are based on:                */
  118. /* Dr. Dobbs Journal --- Oct. 1989.             */
  119. /* Article on LZW Data Compression by Mark R. Nelson     */
  120. /********************************************************/
  121.  
  122. #define MAX_BITS 12        /* this is max for GIF. */
  123.  
  124. #define TABLE_SIZE 5123        /* this is max for 12-bit codes */
  125. #define TABLE_HASH_SHIFT 2    /* size < 4095 + (4095 >> shift) */
  126.  
  127. /* State of LZW encoder */
  128. typedef struct code_entry_s {
  129.     int    code_value;
  130.     ushort    prefix_code;
  131.     byte    append_character;
  132. } code_entry;
  133. typedef struct lzw_encoder_s {
  134.     int    bits;
  135.     ushort    Max_Code;
  136.     ushort    Clear_code;
  137.     ushort    next_code;
  138.     FILE    *file;
  139.     code_entry    *table;
  140.     ushort    string_code;
  141.     /* State of output buffer */
  142.     byte    output_bit_buffer;
  143.     int    output_bit_count;    /* # of valid low-order bits */
  144.                     /* (between 0 and 7) in buffer */
  145.     uint    byte_count;
  146.     byte    gif_buffer[260];
  147. } lzw_encoder;
  148.  
  149. /* Initialize LZW encoder */
  150. private void lzw_set_bits(P2(register lzw_encoder _ss *, int));
  151. private void lzw_reset(P1(register lzw_encoder _ss *));
  152. private int
  153. lzw_init(register lzw_encoder _ss *pe, int bits, FILE *file)
  154. {    lzw_set_bits(pe, bits);
  155.     pe->Clear_code = (1 << bits);
  156.     pe->file = file;
  157.     pe->byte_count = 1;
  158.     pe->output_bit_count = 0;
  159.     pe->output_bit_buffer = 0;
  160.     pe->table = (code_entry *)gs_malloc(TABLE_SIZE, sizeof(code_entry), "GIF code table");
  161.  
  162.     if ( pe->table == 0 )
  163.         return_error(gs_error_VMerror);    /* can't allocate buffers */
  164.  
  165.     lzw_reset(pe);
  166.     pe->string_code = 0;
  167.     return 0;
  168. }
  169. /* Establish the width of the code in bits */
  170. private void
  171. lzw_set_bits(register lzw_encoder _ss *pe, int bits)
  172. {    pe->bits = bits;
  173.     pe->Max_Code = (1 << (bits+1)) - 1;
  174. }
  175. /* Reset the encoding table */
  176. private void
  177. lzw_reset(register lzw_encoder _ss *pe)
  178. {    int index;
  179.     for ( index = 0; index < TABLE_SIZE; index++ )
  180.         pe->table[index].code_value = -1;
  181.     pe->next_code = pe->Clear_code + 2;
  182. }
  183.  
  184. /* Put out (data) of length (bits) to GIF buffer */
  185. private void
  186. lzw_putc(register lzw_encoder _ss *pe, uint data)
  187. {    int bits = pe->bits + 1;    /* output width */
  188.     ulong buffer = pe->output_bit_buffer | ((ulong)data << pe->output_bit_count);
  189.     pe->output_bit_count += bits;
  190.     while ( pe->output_bit_count >= 8 )
  191.     {    /* putc(output_bit_buffer >> 24, file); */
  192.         pe->gif_buffer[pe->byte_count] = (byte)buffer;  /* low byte */
  193.         buffer >>= 8;
  194.         pe->output_bit_count -= 8;
  195.         pe->byte_count++;
  196.         if ( pe->byte_count == 256 )
  197.         {    pe->byte_count = 1;
  198.             pe->gif_buffer[0] = 255;  /* byte count for block */
  199.             fwrite(pe->gif_buffer, 1, 256, pe->file);
  200.         }
  201.     }
  202.     pe->output_bit_buffer = (byte)buffer;
  203. }
  204.  
  205. /* Finish encoding, and flush the buffers. */
  206. private void
  207. lzw_finish(register lzw_encoder _ss *pe)
  208. {    lzw_putc(pe, pe->string_code);    /* output last code */
  209.     lzw_putc(pe, pe->Clear_code+1);    /* output eof code */
  210.     lzw_putc(pe, 0);    /* force out last code */
  211.     if ( pe->byte_count != 1 )
  212.     {    pe->gif_buffer[0] = pe->byte_count;
  213.         fwrite(pe->gif_buffer, 1, pe->byte_count+1, pe->file);
  214.     }
  215. }
  216.  
  217. /* Terminate LZW encoder. */
  218. private void
  219. lzw_exit(register lzw_encoder _ss *pe)
  220. {    gs_free((char *)pe->table, TABLE_SIZE, sizeof(code_entry), "GIF code table");
  221. }
  222.  
  223. /* Get the next (depth) bits from the pixel buffer. */
  224. /* Note that 8 % depth == 0. */
  225. /* Free variables: bits_left, bit_buffer, next, depth, depth_mask. */
  226. #define lzw_getc()\
  227.  (bits_left =\
  228.   (bits_left == 0 ?\
  229.    (bit_buffer = *(next++), 8 - (depth)) :\
  230.    bits_left - (depth)),\
  231.   (bit_buffer >> bits_left) & (depth_mask))
  232.  
  233. /* Output 1 row of data in GIF (LZW) format. */
  234. private void
  235. lzw(byte *from, byte *end, register lzw_encoder _ss *pe, int depth)
  236. {    int bits_left = 0;
  237.     uint bit_buffer;
  238.     byte *next = from;
  239.     uint depth_mask = (1 << depth) - 1;
  240.  
  241.     if ( pe->next_code == (pe->Clear_code + 2))    /* first time through */
  242.     {    pe->string_code = lzw_getc();
  243.     }
  244.  
  245.     while ( next < end || bits_left >= depth )
  246.     {    uint    data = lzw_getc();  /* actually only a byte */
  247.  
  248.         /* Hash to find a match for the prefix+char */
  249.         /* string in the string table */
  250.  
  251.         ushort    hash_prefix = pe->string_code;
  252.         int    index = (data << 4) ^ hash_prefix;
  253.         int    hash_offset;
  254.         register code_entry *pce;
  255.  
  256.         index += index >> TABLE_HASH_SHIFT;
  257.         if ( index == 0 )
  258.             hash_offset = 1;
  259.         else
  260.             hash_offset = TABLE_SIZE - index;
  261.  
  262.         while ( 1 )
  263.         {    pce = &pe->table[index];
  264.             if ( pce->code_value == -1 )
  265.                 break;
  266.             if ( pce->prefix_code == hash_prefix && 
  267.                  pce->append_character == data )
  268.                 break;
  269.             index -= hash_offset;
  270.             if ( index < 0 )
  271.                 index += TABLE_SIZE;
  272.         }
  273.         if ( pce->code_value != -1 )
  274.             pe->string_code = pce->code_value;
  275.         else
  276.         {    /* Make a new entry */
  277.             pce->code_value = pe->next_code++;
  278.             pce->prefix_code = pe->string_code;
  279.             pce->append_character = data;
  280.  
  281.             lzw_putc(pe, pe->string_code);
  282.  
  283.             if ( pe->next_code > (pe->Max_Code + 1) )
  284.             {    /* Increment the width of the code */
  285.                 if ( pe->bits+1 >= MAX_BITS )
  286.                 {    /* output clear code first*/
  287.                     lzw_putc(pe, pe->Clear_code);
  288.                     pe->bits = (depth == 1 ? 2 : depth);
  289.                     lzw_reset(pe);
  290.                 }
  291.                 else
  292.                     pe->bits++;
  293.                 lzw_set_bits(pe, pe->bits);
  294.             }
  295.             pe->string_code = data;
  296.         }
  297.     }
  298. }
  299.  
  300.  
  301. /* Open the device.  The only reason for this routine is */
  302. /* to print the obnoxious copyright notice. */
  303. private int
  304. gif_open(gx_device *pdev)
  305. {    int code = gdev_prn_open(pdev);
  306.     if ( code < 0 ) return code;
  307.  
  308.     /* Put the message on stderr so it doesn't interfere with */
  309.     /* possible piped output. */
  310.     fprintf(stderr, "The Graphics Interchange Format(c) is\n");
  311.     fprintf(stderr, "the Copyright Property of CompuServe Incorporated.\n");
  312.     fprintf(stderr, "GIF(sm) is a Service Mark property of CompuServe Incorporated.\n");
  313.  
  314.     return 0;
  315. }
  316.  
  317.  
  318. /* Write a page to a file in GIF format. */
  319. private int
  320. gif_print_page(gx_device_printer *pdev, FILE *file)
  321. {    int raster = gdev_prn_raster(pdev);
  322.     ushort height = pdev->height;
  323.     int depth = pdev->color_info.depth;
  324.     ushort gif_width = raster * (8 / depth); /* decoders want the width */
  325.                         /* on a byte boundary */
  326.     byte *row = (byte *)gs_malloc(raster * 2, 1, "gif file buffer");
  327.     byte *end = row + raster;
  328.     gif_header header;
  329.     image_descriptor header_desc;
  330.     lzw_encoder encoder;
  331.     int y;
  332.     int code = 0;            /* return code */
  333.  
  334.     if ( row == 0 )            /* can't allocate row buffer */
  335.         return_error(gs_error_VMerror);
  336.     code = lzw_init(&encoder, (depth == 1 ? 2 : depth), file);
  337.     if ( code < 0 )
  338.         return code;
  339.  
  340.     /* Set up the header. */
  341.  
  342.     memcpy(header.signature, "GIF", 3);
  343.     memcpy(header.version, "87a", 3);
  344.     assign_ushort(header.width, gif_width);
  345.     assign_ushort(header.height, height);
  346. /*    header.flags.globalcolor = TRUE;    */
  347. /*    header.flags.colorres = depth-1;    */
  348. /*    header.flags.sort = FALSE;        */
  349. /*    header.flags.colorsize = depth-1;    */
  350.     header.flags =
  351.         (1 << globalcolor_shift) +
  352.         ((depth - 1) << colorres_shift) +
  353.         (0 << sort_shift) +
  354.         ((depth - 1) << colorsize_shift);
  355.     header.background = 0;
  356.     header.aspect = 0;
  357.     
  358.     /* Write the header, on the first page only. */
  359.  
  360.     if ( gdev_prn_file_is_new(pdev) )
  361.     {
  362.         if ( fwrite(&header, 1, 13, file) < 13 )
  363.         {    code = gs_error_ioerror;
  364.             goto gif_done;
  365.         }
  366.  
  367.         /* Write the header global color palette. */
  368.  
  369.         if ( pc_write_palette((gx_device *)pdev, 1 << depth, file) < 0 )
  370.         {    code = gs_error_ioerror;
  371.             goto gif_done;
  372.         }
  373.     }
  374.  
  375.     header_desc.left_pos = 0;
  376.     header_desc.top_pos = 0;
  377.     assign_ushort(header_desc.width, gif_width);
  378.     assign_ushort(header_desc.height, height);
  379. /*    header_desc.flags.localcolor = TRUE;    */
  380. /*    header_desc.flags.interlace = FALSE;    */
  381. /*    header_desc.flags.sort = FALSE;        */
  382. /*    header_desc.flags.localsize = depth - 1;*/
  383.  
  384.     header_desc.flags =
  385.         (1 << globalcolor_shift) +
  386.         ((depth - 1) << colorsize_shift);
  387.  
  388.     /* Write the header image descriptor. */
  389.  
  390.     fputc(0x2c,file);        /* start with separator */
  391.     if ( fwrite(&header_desc, 1, 9, file) < 9 )
  392.        {    code = gs_error_ioerror;
  393.         goto gif_done;
  394.        }
  395.   
  396.     /* Write the local color palette. */
  397.  
  398.     if ( pc_write_palette((gx_device *)pdev, 1 << depth, file) < 0 )
  399.        {    code = gs_error_ioerror;
  400.         goto gif_done;
  401.        }
  402.  
  403.     fputc(encoder.bits, file);        /* start with code size */
  404.  
  405.     lzw_putc(&encoder, encoder.Clear_code);    /* output clear code first*/
  406.  
  407.     /* Dump the contents of the image. */
  408.     for ( y = 0; y < height; y++ ) 
  409.        {    gdev_prn_copy_scan_lines(pdev, y, row, raster);
  410.         lzw(row, end, &encoder, depth);
  411.        }
  412.  
  413.     lzw_finish(&encoder);
  414.         fputc(0, file);
  415.  
  416. gif_done:
  417.     lzw_exit(&encoder);
  418.     gs_free((char *)row, raster * 2, 1, "gif file buffer");
  419.     return code;
  420. }
  421.  
  422. /* Close the device, writing an end-of-file mark. */
  423. private int
  424. gif_close(gx_device *pdev)
  425. {
  426.     FILE *file = ((gx_device_printer *)pdev)->file;
  427.  
  428.     if ( file != NULL )
  429.     {    fputc(0x3b, file);    /* EOF indicator */
  430.     }
  431.  
  432.     return gdev_prn_close(pdev);
  433. }
  434.